/// Creator:      gaoruishan
/// CreatDate:    2025-06-04
/// Description:  Rfid绑定相关
Class Nur.MNISV3.Service.RfidBindRegNo Extends %RegisteredObject [ Not ProcedureBlock ]
{

///   w ##class(Nur.MNISV3.Service.RfidBindRegNo).GetRfidDataV4(5,3)
ClassMethod GetRfidDataV4(wardId As %String, locId As %String = "", userId = "")
{
    n (wardId,locId,userId)
    s $zt="errGetRfidDataV4"
    s ^GRS("GetRfidDataV4")=$lb(wardId,locId,userId)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    //调用纯净版患者列表，
    s patListObj=##class(%ResultSet).%New("Nur.MNISV3.Service.Patient:GetPatList")
    d patListObj.Execute(wardId,userId,"Ward",locId)
    s patInfoList=##class(%ListOfDataTypes).%New()
    s screeningItems=##class(%ListOfDataTypes).%New()
    while(patListObj.Next()){
       s patinfo= ##class(Nur.MNISV3.Common.Utils).ResultSet(patListObj)
       s episodeId=patListObj.GetDataByName("episodeId")
       s regNo=##class(Nur.CommonInterface.Patient).getRegNo(episodeId)
       //基本患者信息
       s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
      
        s rfidBindingData=##class(%ListOfDataTypes).%New()
        s RfidId=$o(^CF.NUR.MNISV3.RfidBindRegNoI("BindId"," "_regNo,""))
        i RfidId'="" {
            s BindingData=##class(%ArrayOfDataTypes).%New()
            d BindingData.SetAt($g(bindUser),"bindUser")
            d BindingData.SetAt($g(bindTime),"bindTime")
            d BindingData.SetAt($g(bindDate),"bindDate")
            d BindingData.SetAt($g(unbindDate),"unbindDate")
            d BindingData.SetAt($g(unbindTime),"unbindTime")
            d BindingData.SetAt($g(unbindUser),"unbindUser")
            //是否激活  默认 1是绑定 0解绑
            d BindingData.SetAt("1","active")
            d BindingData.SetAt($g(regNo),"regNo")
            s RfidId=$replace(RfidId," ","")
            s ID=$o(^CF.NUR.MNISV3.RfidBindRegNoI("BindId"," "_regNo," "_RfidId,""))
            i ID'="" {
                s objBind=##class(CF.NUR.MNISV3.RfidBindRegNo).%OpenId(ID)
                i objBind.Type="RFID" {
                    //绑定id
                    d BindingData.SetAt($g(ID),"ID")
                    //设备码
                    d BindingData.SetAt($g(RfidId),"barcode")
                    d rfidBindingData.Insert(BindingData)
                }
            }
        }
        d patinfo.SetAt(rfidBindingData,"rfidBindingData")
        d patInfoList.Insert(patinfo)
    }

    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(patInfoList),"patients")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)

errGetRfidDataV4
    s Msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(Msg,"100021","-1")
}

/// w ##class(Nur.MNISV3.Service.RfidBindRegNo).BindRegNo(5,3)
ClassMethod BindRfidV4(barcode, episodeId, userId = "") As %String
{
    n (barcode,episodeId, userId )
    s $zt="errBindRfidV4"
    s rfidId=barcode
    s ^GRS("BindRfidV4")=$lb(barcode,episodeId, userId )
    s msg="成功",msgcode="199999",Status="0"
    i barcode["请扫描" q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(barcode,"555555","-1")
    s regNo=##class(Nur.CommonInterface.Patient).getRegNo(episodeId)
    s bindId=$o(^CF.NUR.MNISV3.RfidBindRegNoI("BindId"," "_regNo,""))
    
    s bindRfid=$o(^CF.NUR.MNISV3.RfidBindRegNoI("RfidId"," "_$$ALPHAUP^SSUTIL4(rfidId),""))
    s bindId=$replace(bindId," ",""),bindRfid=$replace(bindRfid," ","")
    i (bindId'="")||(bindRfid'="") q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("已绑定过，请勿重复操作！","555555","-1")
    
    s result=##class(CF.NUR.MNISV3.RfidBindRegNo).Save(rfidId,regNo,userId,"RFID")
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errBindRfidV4
    s msg=$ze,msgcode="199999",Status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,Status)
}

/// w ##class(Nur.MNISV3.Service.RfidBindRegNo).UnBindRegNo(5,3)
ClassMethod UnbindRfidV4(id, userId = "") As %String
{
    n (id,userId)
    s ^GRS("UnbindRfidV4")=$lb(userId)
    s $zt="errUnbindRfidV4"
    s msg="成功",msgcode="199999",Status="0"
    i id="" q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("未绑定过，请勿操作！","555555","-1")
    s bindId= id ;$o(^CF.NUR.MNISV3.RfidBindRegNoI("BindId"," "_regNo,rfidId,""))
    s result=##class(CF.NUR.MNISV3.RfidBindRegNo).Delete(bindId)
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(result),"result")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errUnbindRfidV4
    s msg=$ze,msgcode="199999",Status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,Status)
}

/// 输液绑定--先暂时共用一个表，根据需要可以新建表
/// w ##class(Nur.MNISV3.Service.RfidBindRegNo).GetInfusionBindingDataV4(5,3)
ClassMethod GetInfusionBindingDataV4(wardId As %String, locId As %String = "", userId = "")
{
    n (wardId,locId,userId)
    s $zt="errGetInfusionBindingDataV4"
    s ^GRS("GetInfusionBindingDataV4")=$lb(wardId,locId,userId)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    //调用纯净版患者列表，
    s patListObj=##class(%ResultSet).%New("Nur.MNISV3.Service.Patient:GetPatList")
    d patListObj.Execute(wardId,userId,"Ward",locId)
    s patInfoList=##class(%ListOfDataTypes).%New()
    s screeningItems=##class(%ListOfDataTypes).%New()
    while(patListObj.Next()){
       s patinfo= ##class(Nur.MNISV3.Common.Utils).ResultSet(patListObj)
       s episodeId=patListObj.GetDataByName("episodeId")
       s regNo=##class(Nur.CommonInterface.Patient).getRegNo(episodeId)
       //基本患者信息
       s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
      
        s infusionBindingData=##class(%ListOfDataTypes).%New()
        s RfidId=$o(^CF.NUR.MNISV3.RfidBindRegNoI("BindId"," "_regNo,""))
        i RfidId'="" {
            s BindingData=##class(%ArrayOfDataTypes).%New()
            d BindingData.SetAt($g(bindUser),"bindUser")
            d BindingData.SetAt($g(bindTime),"bindTime")
            d BindingData.SetAt($g(bindDate),"bindDate")
            d BindingData.SetAt($g(unbindDate),"unbindDate")
            d BindingData.SetAt($g(unbindTime),"unbindTime")
            d BindingData.SetAt($g(unbindUser),"unbindUser")
            //是否激活  默认 1是绑定 0解绑
            d BindingData.SetAt("1","active")
            d BindingData.SetAt($g(regNo),"regNo")
            s RfidId=$replace(RfidId," ","")
            s ID=$o(^CF.NUR.MNISV3.RfidBindRegNoI("BindId"," "_regNo," "_RfidId,""))
            i ID'="" {
                s objBind=##class(CF.NUR.MNISV3.RfidBindRegNo).%OpenId(ID)
                i objBind.Type="INF" {
                    //绑定id
                    d BindingData.SetAt($g(ID),"ID")
                    //设备码
                    d BindingData.SetAt($g(RfidId),"barcode")
                    d infusionBindingData.Insert(BindingData)
                }
            }
        
        }
        d patinfo.SetAt(infusionBindingData,"infusionBindingData")
        d patInfoList.Insert(patinfo)
    }

    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(patInfoList),"patients")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetInfusionBindingDataV4
    s Msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(Msg,"100021","-1")
}

/// w ##class(Nur.MNISV3.Service.RfidBindRegNo).BindInfusionV4(5,3)
ClassMethod BindInfusionV4(barcode, episodeId, userId = "") As %String
{
    n (barcode,episodeId,userId)
    s $zt="errBindInfusionV4"
    s rfidId=barcode
    s ^GRS("BindInfusionV4")=$lb(barcode,episodeId,userId)
    s msg="成功",msgcode="199999",Status="0"
    i barcode["请扫描" q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(barcode,"555555","-1")
    s regNo=##class(Nur.CommonInterface.Patient).getRegNo(episodeId)
    s bindId=$o(^CF.NUR.MNISV3.RfidBindRegNoI("BindId"," "_regNo,""))
    
    s bindRfid=$o(^CF.NUR.MNISV3.RfidBindRegNoI("RfidId"," "_$$ALPHAUP^SSUTIL4(rfidId),""))
    s bindId=$replace(bindId," ",""),bindRfid=$replace(bindRfid," ","")
    i (bindId'="")||(bindRfid'="") q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("已绑定过，请勿重复操作！","555555","-1")
    
    s result=##class(CF.NUR.MNISV3.RfidBindRegNo).Save(rfidId,regNo,userId,"INF")
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errBindInfusionV4
    s msg=$ze,msgcode="199999",Status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,Status)
}

/// w ##class(Nur.MNISV3.Service.RfidBindRegNo).UnbindInfusionV4(5,3)
ClassMethod UnbindInfusionV4(id, userId = "") As %String
{
    n (id,userId)
    s ^GRS("UnbindInfusionV4")=$lb(userId)
    s $zt="errUnbindInfusionV4"
    s msg="成功",msgcode="199999",Status="0"
    i id="" q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("未绑定过，请勿操作！","555555","-1")
    s bindId= id ;$o(^CF.NUR.MNISV3.RfidBindRegNoI("BindId"," "_regNo,rfidId,""))
    s result=##class(CF.NUR.MNISV3.RfidBindRegNo).Delete(bindId)
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(result),"result")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errUnbindInfusionV4
    s msg=$ze,msgcode="199999",Status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,Status)
}

}
